//
//  MainTabBarController.swift
//  FirstSwiftProduct
//
//  Created by 范泽华 on 2017/10/10.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    /**
     *  title的字体大小
     */
    var titleFont = UIFont.systemFont(ofSize: 10)
    /**
     *  title未选中的字体颜色
     */
    var titleNormalColor = UIColor.black
    /**
     *  title选中的字体颜色
     */
    var titleSelectColor = UIColor.blue
    
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        for child in tabBar.subviews as [UIView] {
            if child.isKind(of: NSClassFromString("UITabBarButton")!) {
//                child.removeFromSuperview()
            }
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let backView = UIView()
        backView.frame = self.view.frame
        backView.height = kTabBarHight
//        backView.backgroundColor = UIColor.colorWithHexString_alpha(alpha: 1.0, color: "#4cbaff")
        backView.backgroundColor = UIColor.white
        self.tabBar.insertSubview(backView, at: 0)
        self.tabBar.isOpaque = true
        
        let attri = [NSFontAttributeName:UIFont.systemFont(ofSize: 15),NSForegroundColorAttributeName:UIColor.colorWithHexString(color: "#b3ffff")]
        
        self.tabBarItem.setTitleTextAttributes(attri, for: UIControlState.normal)
        self.tabBar.tintColor = UIColor.colorWithHexString(color: "#c6aa69")
        self .initChildViewControllers()
        // Do any additional setup after loading the view.
    }

    
    func initChildViewControllers() -> Void {
        
        //tabbar基类控制器的创建
        let homePageVC = HomePageController()
        
        addChildViewControllerVC(childController: FzhNavigationController.init(rootViewController: homePageVC), normalImage: UIImage.init(named: "main")!, selectImage: UIImage.init(named: "main_select")!, tabBarTitle: "首页")
        
        let financialVC = FinancialViewController()
        addChildViewControllerVC(childController: FzhNavigationController.init(rootViewController: financialVC), normalImage: UIImage.init(named: "invest")!, selectImage: UIImage.init(named: "invest_select")!, tabBarTitle: "理财")
        
        let mineVC = MineViewController()
        
        addChildViewControllerVC(childController: FzhNavigationController.init(rootViewController: mineVC), normalImage: UIImage.init(named: "mine_tabbar")!, selectImage: UIImage.init(named: "mine_tabbar_hl")!, tabBarTitle: "我的")

        let living = LivingViewController()
        
        addChildViewControllerVC(childController: FzhNavigationController.init(rootViewController: living), normalImage: UIImage.init(named: "user")!, selectImage: UIImage.init(named: "user_select")!, tabBarTitle: "更多")
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /**
     *  为TabBar添加Viewcontroller
     *
     *  @param childController VC
     *  @param normalImage 未选中的图片
     *  @param selectImage 选中的图片
     *  @param tabBartitle     VC的标题
     */
    func addChildViewControllerVC(childController:UIViewController,normalImage:UIImage,selectImage:UIImage,tabBarTitle:String) -> Void {
        /**
         *  未选中时的图片
         */
        childController.tabBarItem.image = normalImage.withRenderingMode(.alwaysOriginal)//以原图颜色渲染
        /**
         *  选中时的图片
         */
        childController.tabBarItem.selectedImage = selectImage.withRenderingMode(.alwaysOriginal)
        
        let normallAttr = [NSFontAttributeName:self.titleFont,NSForegroundColorAttributeName:UIColor.colorWithHexString_alpha(alpha: 1.0, color: "#666666")] as [String : Any]
        childController.tabBarItem.setTitleTextAttributes(normallAttr, for: UIControlState.normal)
        
        let selectAttr = [NSForegroundColorAttributeName:UIColor.colorWithHexString(color: "#d9261c"),NSFontAttributeName:self.titleFont] as [String : Any]
        childController.tabBarItem.setTitleTextAttributes(selectAttr, for: UIControlState.selected)
        
        childController.tabBarItem.title = tabBarTitle
        
        /**
         *  把VC加到TabBarController
         */
        self .addChildViewController(childController)
        
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
